|
|
|
|
|
|
|
For the first keyboard filtering example, you set up a textbox control that accepts only numerals zero through nine. First, add a label control and a textbox control to the form. Set the caption property of the label control to Numbers. Set the Name property of the textbox control to txtNumber and set the text property to blank. Your form should resemble the one in Figure 5.1. |
|
|
|
|
|
|
|
|
Figure 5.1.
Adding the Numbers input control. |
|
|
|
|
|
|
|
|
Save and run the program. You can enter any type of data in the textbox that you wishnumbers, letters, spaces, and so on. Now you add a small bit of code that filters out all but the numerals zero through nine. You do this by using the textbox control KeyPress event. |
|
|
|
|
|
|
|
|
The KeyPress event occurs each time a user presses a key while the field has the focus. Each time a key is pressed while the cursor is in the textbox control, the ASCII value of the key is sent to the KeyPress event where you can evaluate it and act accordingly. |
|
|
|
|
| |
|
|
|
|
Each key on the keyboard has an ASCII (American Standard Code for Information Interchange) numeric value. Your Visual Basic 5 documentation has a list of the ASCII codes for each key on the keyboard. |
|
|
|
|
|
|
|
|
|
|
In this example, you want to ignore any keystroke that is not a 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. To do this, you need to add a small bit of code (see Listing 5.1) to the KeyPress event of the txtNumbers textbox. |
|
|
|
|
|
|
|
|
Listing 5.1. Limiting data entry in the Keypress event. |
|
|
|
|
|
|
|
|
Private Sub txtNumber_KeyPress(KeyAscii As Integer)
'
Dim strValid As String
'
strValid = "0123456789"
' |
|
|
|
|
|